package com.example.single;

/**
 * description: LazySingleton <br>
 * date: 2020/7/24 13:48 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class LazySingleton {

    private static volatile LazySingleton lazySingleton = null ;

    private LazySingleton(){

    }

    public static LazySingleton getInstance(){
        //判断实例是否为空，为空就实例化创建
        if (null == lazySingleton){
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LazySingleton.class){
                if (null == lazySingleton) {
                    lazySingleton = new LazySingleton();
                }
            }
        }
        //不为空就直接返回
        return lazySingleton;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                System.out.println(LazySingleton.getInstance());
            }).start();
        }
    }
}
